package jnpf.upload.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.entity.FileDetail;
import jnpf.file.FileUploadApi;
import jnpf.model.MinioCopyObjectModel;
import jnpf.model.FileListVO;
import jnpf.model.upload.UploadFileModel;
import jnpf.util.*;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedList;
import java.util.List;

/**
 * 文件上传类
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021-07-20
 */
@RequestMapping(value = "/upload")
@RestController
public class FileUploadController implements FileUploadApi {

    /**
     * 文件上传
     *
     * @param multipartFile
     * @param folderName
     * @param fileName
     * @return
     */
    @Override
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo uploadFile(@RequestPart("multipartFile") MultipartFile multipartFile, @RequestParam("folderName") String folderName, @RequestParam("fileName") String fileName) {
        //根据文件保存类型选择对应的方法
        return FileUploadUtils.uploadFile(multipartFile, folderName, fileName);
    }

    /**
     * 文件上传
     *
     * @param multipartFile
     * @param folderName
     * @param fileName
     * @return
     */
    @Override
    @NoDataSourceBind()
    @PostMapping(value = "/uploadFiles", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo uploadFile(@RequestPart("multipartFile") MultipartFile multipartFile, @RequestParam("folderName") String folderName, @RequestParam("type") String type, @RequestParam("fileName") String fileName) {
        //根据文件保存类型选择对应的方法
        return FileUploadUtils.uploadFile(multipartFile, folderName + type + "/", fileName);
    }

    /**
     * 拷贝文件到其他Minio下
     *
     * @param model
     */
    @Override
    @PostMapping("/copyObject")
    public void copyObject(@RequestBody MinioCopyObjectModel model) {
//        GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).copyObject(model.getFolderName(), model.getObjectName(), model.getCopyTofolderName(), model.getCopyToObjectName());
    }

    @Override
    @NoDataSourceBind
    @GetMapping(value = "/getByte/{fileName}/{folderName}")
    public byte[] getByte(@PathVariable("fileName") String fileName, @PathVariable("folderName") String folderName) {
        return FileUploadUtils.downloadFileByte(folderName + "/", fileName, false);
    }

    @Override
    @NoDataSourceBind
    @GetMapping(value = "/getByte")
    public List<FileListVO> getFileList(@RequestParam("folderName") String folderName) {
        List<FileListVO> data = new LinkedList<>();
        List<FileListVO> fileList = FileUploadUtils.getFileList(folderName);
        if (fileList != null) {
            for (int i = 0; i < fileList.size(); i++) {
                FileListVO fileDetail = fileList.get(i);
                FileListVO fileModel = new FileListVO();
                fileModel.setFileId(i + "");
                fileModel.setFileType(fileDetail.getFileType());
                fileModel.setFileName(fileDetail.getFileName());
                fileModel.setFileTime(fileDetail.getFileTime());
                fileModel.setFileSize(fileDetail.getFileSize() == null ? "0" : String.valueOf(fileDetail.getFileSize()));
                data.add(fileModel);
            }
        }
        return data;
    }

    @Override
    @PostMapping("/downToLocal")
    public void downToLocal(@RequestBody UploadFileModel model) {
        FileUploadUtils.downloadFile(model.getFolderName(), model.getObjectName());
    }

    @Override
    @PostMapping("/downFile")
    public void downFile(@RequestBody UploadFileModel model) {
        FileUploadUtils.downLocal(model.getFolderName(), model.getFilePath(), model.getObjectName());
    }

    @Override
    @NoDataSourceBind
    @PostMapping("/downFileByte")
    public byte[] downFileByte(@RequestBody UploadFileModel model) {
        return FileUploadUtils.downloadFileByte(model.getFolderName(), model.getObjectName(), false);
    }

    @Override
    @GetMapping("/getFile/{fileId}")
    public FileInfo getFile(@PathVariable("fileId") String fileId) {
        return null;
//        FileDetail detail = FileUploadUtils.getFileDetail(fileId);
//        return BeanUtil.copyProperties(detail,FileInfo.class,"attr");
    }


}
